package com.tedu.cooshark.controller;

import com.github.pagehelper.PageInfo;
import com.tedu.cooshark.entity.Product;
import com.tedu.cooshark.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 2022/3/26  10:40
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    private String dirPath = "D:/product";
    @Autowired
    private ProductService productService;

    public ProductController() {
    }

    @RequestMapping("/selectAll")
    private List<Product> selectAll(){
        return productService.selectAll();
    }

    @RequestMapping("/selectAllPage")
    private PageInfo<Product> selectAllPage(int page, int offset){
       return productService.selectAllPage(page,offset);
    }
    @RequestMapping("/addproduct")
    private int addproduct(@RequestBody Product product){
        System.out.println("新增的商品;"+product);
        return productService.insertProduct(product);
    }
    @RequestMapping("/deleteByid")
    private int deleteByid(@RequestBody Product product){
        System.out.println("删除的商品;"+product);
        return productService.deleteByid(product);
    }
    @RequestMapping("/deleteProductByids")
    private int deleteProductByids(@RequestBody List<Product> products){
        return productService.deleteProductByids(products);
    }
    @RequestMapping("/updateProduct")
    private int updateProduct(@RequestBody Product product){
        System.out.println("修改的商品;"+product);
        product.setCreateTime(new Date());
        return productService.updateProduct(product);
    }
    @RequestMapping("/uploadProduct")
    public String upload(MultipartFile picFile) throws IOException {
        System.out.println("执行了文件上传!");
        //得到原始文件名
        String fileName=picFile.getOriginalFilename();
        //取得上传文件的后缀
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //获取一个唯一文件id加上字符串生成心的文件名
        fileName = UUID.randomUUID()+suffix;
        //设置一个存放文件的路径
        File dirFile= new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //文件名加存放路径
        String filePath = dirPath+"/"+fileName;
        //将文件放入路径中
        picFile.transferTo(new File(filePath));
        return "/"+fileName;
    }
    @RequestMapping("/removeProduct")
    public void remove(String name){
        System.out.println("执行了文件删除");
        String filePath = dirPath+name;
        new File(filePath).delete();//删除文件
    }

    public String getDirPath() {
        return this.dirPath;
    }

    public ProductService getProductService() {
        return this.productService;
    }

    public void setDirPath(String dirPath) {
        this.dirPath = dirPath;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof ProductController)) return false;
        final ProductController other = (ProductController) o;
        if (!other.canEqual((Object) this)) return false;
        final Object this$dirPath = this.getDirPath();
        final Object other$dirPath = other.getDirPath();
        if (this$dirPath == null ? other$dirPath != null : !this$dirPath.equals(other$dirPath)) return false;
        final Object this$productService = this.getProductService();
        final Object other$productService = other.getProductService();
        if (this$productService == null ? other$productService != null : !this$productService.equals(other$productService))
            return false;
        return true;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof ProductController;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $dirPath = this.getDirPath();
        result = result * PRIME + ($dirPath == null ? 43 : $dirPath.hashCode());
        final Object $productService = this.getProductService();
        result = result * PRIME + ($productService == null ? 43 : $productService.hashCode());
        return result;
    }

    public String toString() {
        return "ProductController(dirPath=" + this.getDirPath() + ", productService=" + this.getProductService() + ")";
    }
}
